package com.example.smssender.contactimport;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.example.smssender.R;

/**
 * 导入预览对话框
 */
public class ContactImportDialog extends Dialog {
    
    private ImportResult importResult;
    private OnImportListener listener;
    
    public interface OnImportListener {
        void onImport();
        void onCancel();
    }
    
    public ContactImportDialog(@NonNull Context context, ImportResult importResult, OnImportListener listener) {
        super(context);
        this.importResult = importResult;
        this.listener = listener;
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_import_preview);
        
        TextView textTotalCount = findViewById(R.id.textTotalCount);
        TextView textValidCount = findViewById(R.id.textValidCount);
        TextView textInvalidCount = findViewById(R.id.textInvalidCount);
        TextView textDuplicateCount = findViewById(R.id.textDuplicateCount);
        TextView textErrorDetails = findViewById(R.id.textErrorDetails);
        Button btnViewDetails = findViewById(R.id.btnViewDetails);
        Button btnCancel = findViewById(R.id.btnCancel);
        Button btnImport = findViewById(R.id.btnImport);
        
        if (importResult != null) {
            // 显示统计信息
            textTotalCount.setText("共找到 " + importResult.getTotalCount() + " 条记录");
            textValidCount.setText(importResult.getSuccessCount() + " 条");
            textInvalidCount.setText(importResult.getFailCount() + " 条");
            textDuplicateCount.setText(importResult.getDuplicateCount() + " 条");
            
            // 显示错误详情
            if (!importResult.getErrors().isEmpty()) {
                StringBuilder errorText = new StringBuilder();
                int maxErrors = Math.min(importResult.getErrors().size(), 10);
                for (int i = 0; i < maxErrors; i++) {
                    errorText.append("• ").append(importResult.getErrors().get(i)).append("\n");
                }
                if (importResult.getErrors().size() > 10) {
                    errorText.append("... 还有 ").append(importResult.getErrors().size() - 10).append(" 条错误");
                }
                
                textErrorDetails.setText(errorText.toString());
                textErrorDetails.setVisibility(View.VISIBLE);
                btnViewDetails.setVisibility(View.VISIBLE);
                
                btnViewDetails.setOnClickListener(v -> {
                    // 显示完整错误列表
                    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(getContext());
                    builder.setTitle("错误详情");
                    StringBuilder fullErrorText = new StringBuilder();
                    for (String error : importResult.getErrors()) {
                        fullErrorText.append("• ").append(error).append("\n");
                    }
                    builder.setMessage(fullErrorText.toString());
                    builder.setPositiveButton("确定", null);
                    builder.show();
                });
            } else {
                textErrorDetails.setVisibility(View.GONE);
                btnViewDetails.setVisibility(View.GONE);
            }
            
            // 如果没有有效数据，禁用导入按钮
            if (importResult.getSuccessCount() == 0) {
                btnImport.setEnabled(false);
                btnImport.setText("无可导入数据");
            }
        }
        
        btnCancel.setOnClickListener(v -> {
            if (listener != null) {
                listener.onCancel();
            }
            dismiss();
        });
        
        btnImport.setOnClickListener(v -> {
            if (listener != null) {
                listener.onImport();
            }
            dismiss();
        });
    }
}

